<?php
class Admin_CraftController extends Protox_Controller_Action
{

	public function indexAction()
	{
		$this->_helper->redirector('list', 'craft','admin');
	}

	public function listAction()
	{
		$this->view->pageTitle = 'Gerenciar cargos';

		$craftsDbTable = new Admin_Model_Crafts();

		$form = new Admin_Form_CraftSearch();
		$request = $this->getRequest();

		$this->view->form = $form;

		if ($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				$description = strtolower($form->getValue('description'));

				$select = $craftsDbTable->findCrafts($description);

				if(sizeOf($select)==0) {
					$this->view->priorityMessenger(sprintf('Nenhum cargo encontrado'), 'alert');
				}
			}
		} else {
			$select = $craftsDbTable->getCrafts();
		}

		$paginator = Zend_Paginator::factory($select);
		$page = $this->_getParam('page', 1);
		$paginator->setCurrentPageNumber($page);
		$paginator->setItemCountPerPage(20);

		$this->view->total = count($select);
		$this->view->paginator = $paginator;
	}

	public function addAction()
	{
		$this->view->pageTitle = 'Inserir novo cargo';

		$form = new Admin_Form_CraftAdd();
		$request = $this->getRequest();

		$this->view->form = $form;

		try {
			if ($request->isPost()) {
				if ($form->isValid($request->getPost())) {
					$description = $form->getValue('description');
					$date_created = date('Y-m-d');

					$craftsDbTable = new Admin_Model_Crafts();

					if(!$craftsDbTable->isUniqueCraftDescription($description)) {
						$this->view->priorityMessenger(sprintf('Já exite um cargo cadastrado com a descrição "%s"', $description), 'error');
						return false;
					}

					$craftsDbTable->addCraft($description, $date_created);

					$this->view->priorityMessenger(sprintf('Cargo cadastrado "%s"', $description), 'sucess');

					$this->_helper->redirector('list', 'craft','admin');
				} else {
					$form->populate($this->getRequest()->getPost());
				}
			}

		} catch(Exception $ex) {
			echo $ex->getMessage();
			return false;
		}
	}

	public function updateAction()
	{
		$this->view->pageTitle = 'Editar cargo';

		$form = new Admin_Form_CraftAdd();
		$request = $this->getRequest();

		try {
			
			$id = $this->_getParam('id',0);

			if (!is_null($id)) {
				$craftsDbTable = new Admin_Model_Crafts();
				$select = $craftsDbTable->getCraftById($id);
					
				if(false===$select) {
					$this->view->priorityMessenger(sprintf('Nenhum cargo localizado com id "%s"', $id), 'error');
					return false;

				} else {
					$this->view->form = $form;
					$form->populate($select);
					$this->view->data = $select;

					if ($request->isPost()) {
						if ($form->isValid($request->getPost())) {
							$description = $form->getValue('description');

							$craftsDbTable->updateCraft($id,$description);

							$this->view->priorityMessenger(sprintf('Cargo "%s" editado com sucesso', $select['description']), 'sucess');

							$this->_helper->redirector('list', 'craft','admin');

						} else {
							$form->populate($this->getRequest()->getPost());
						}
					}
				}

			}
		} catch(Exception $ex) {
			return false;
		}
	}

	public function deleteAction()
	{
		$this->view->pageTitle = 'Excluir cargo';

		$form = new Admin_Form_DeleteConfirm();
		$request = $this->getRequest();

		$id = $this->_getParam('id',0);
		if (!is_null($id)) {
			$craftsDbTable = new Admin_Model_Crafts();
			$select = $craftsDbTable->getCraftById($id);
				
			if(false===$select) {
				$this->view->priorityMessenger(sprintf('Nenhum cargo localizado com id "%s"', $id), 'error');
				return false;

			} else {
				$this->view->data = $select;

				if ($request->isPost()) {
					if ($form->isValid($request->getPost())) {

						$respYes = $form->getValue('yes');
						$respNo = $form->getValue('no');

						if(!is_null($respYes)) {
							$this->view->priorityMessenger(sprintf('Cargo excluido "%s"', $select['description']), 'sucess');
							$craftsDbTable->deleteCraft($id);
							$this->_helper->redirector('list', 'craft','admin');
						}

						if(!is_null($respNo)) {
							$this->_helper->redirector('list', 'craft','admin');
						}

					}
				}

				$this->view->form = $form;
			}
		}
	}
	

}